package com.syqc.custom

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import android.text.Spanned
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.syqc.comlib.utils.copyText
import com.syqc.monitor.R
import com.syqc.monitor.databinding.CarBaseTextViewBinding


class CarBaseTextView : LinearLayout {
    private var rightDrawable: Drawable? = null

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    ) {
        initView(context, attrs)
    }

    private lateinit var binding: CarBaseTextViewBinding

    @SuppressLint("CustomViewStyleable")
    private fun initView(context: Context, attrs: AttributeSet?) {
        binding = CarBaseTextViewBinding.inflate(LayoutInflater.from(context), this, true)
        val typedArray: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.left_right_text)
        val rightWeight: Float = typedArray.getFloat(R.styleable.left_right_text_rightWeight, 2f)
        val testSize: Float = typedArray.getFloat(R.styleable.left_right_text_textSize, 14f)
        val background: Drawable? = typedArray.getDrawable(R.styleable.left_right_text_background)
        val leftGravity: Int = typedArray.getInt(R.styleable.left_right_text_leftGravity, 0)
        val rightGravity: Int = typedArray.getInt(R.styleable.left_right_text_rightGravity, 0)
        val leftText: String? = typedArray.getString(R.styleable.left_right_text_leftText)
        val rightText: String? = typedArray.getString(R.styleable.left_right_text_rightText)
        val rightHint: String? = typedArray.getString(R.styleable.left_right_text_rightHint)
        val leftTextColor: Int = typedArray.getColor(
            R.styleable.left_right_text_leftTextColor,
            ContextCompat.getColor(context, R.color.color_333)
        )
        val rightTextColor: Int = typedArray.getColor(
            R.styleable.left_right_text_leftTextColor,
            ContextCompat.getColor(context, R.color.color_666)
        )
        rightDrawable =
            typedArray.getDrawable(R.styleable.left_right_text_rightDrawable)
        val isLineChange = typedArray.getBoolean(R.styleable.left_right_text_isLineChange, false)
        val isLineGone = typedArray.getBoolean(R.styleable.left_right_text_isLineGone, false)
        when (leftGravity) {
            0 -> binding.tvLeft.gravity = Gravity.START or Gravity.CENTER_VERTICAL
            1 -> binding.tvLeft.gravity = Gravity.CENTER
            2 -> binding.tvLeft.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        }
        when (rightGravity) {
            0 -> binding.tvRight.gravity = Gravity.START or Gravity.CENTER_VERTICAL
            1 -> binding.tvRight.gravity = Gravity.CENTER
            2 -> binding.tvRight.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        }
        if (background != null)
            this.background = background
        binding.tvRight.textSize = testSize
        binding.tvLeft.textSize = testSize

        val params = LayoutParams(0, LayoutParams.MATCH_PARENT)
        params.weight = rightWeight
        binding.tvRight.layoutParams = params
        rightDrawable?.apply {
            setBounds(0, 0, minimumWidth, minimumHeight)
            binding.tvRight.setCompoundDrawables(null, null, this, null)
        }

        binding.tvLeft.setTextColor(leftTextColor)
        binding.tvRight.setTextColor(rightTextColor)
        binding.tvLeft.text = leftText ?: ""
        binding.tvRight.text = rightText ?: ""
        binding.tvRight.hint = rightHint ?: ""

        if (isLineChange) {
            binding.bottomLine.isVisible = false
            binding.verticalLine.isVisible = false
            binding.bottomLine12.isVisible = true
            binding.verticalLine16.isVisible = true
        }
        if (isLineGone) {
            binding.bottomLine.isVisible = false
        }

        setOnLongClickListener {
            copyText("${binding.tvLeft.text.trim()} : ${binding.tvRight.text}", context)
            true
        }
        typedArray.recycle()
    }

    fun setLeftTitle(leftText: String) {
        binding.tvLeft.text = leftText
    }

    fun setLeftTitle(leftText: Spanned) {
        binding.tvLeft.setText(leftText)
    }

    fun setLeftTitle(leftTextId: Int) {
        binding.tvLeft.setText(leftTextId)
    }

    fun setRightText(rightText: String) {
        binding.tvRight.text = rightText
    }

    fun setRightText(rightText: Spanned) {
        binding.tvRight.text = rightText
    }

    fun setRightHint(righthint: String) {
        binding.tvRight.hint = righthint
    }

    fun setRightHint(righthintId: Int) {
        binding.tvRight.setHint(righthintId)
    }

    fun setRightText(rightTextId: Int) {
        binding.tvRight.setText(rightTextId)
    }

    fun getRightText(): String {
        return binding.tvRight.text.toString()
    }

    fun setTightColor(isEnabled: Boolean) {
        binding.tvRight.setTextColor(
            if (isEnabled) ContextCompat.getColor(
                context,
                R.color.color_333
            ) else ContextCompat.getColor(context, R.color.color_999)
        )
    }

    override fun setEnabled(enabled: Boolean) {
        super.setEnabled(enabled)
        binding.tvRight.isEnabled = isEnabled
    }

    fun nextVisible(isVisible: Boolean) {
        if (isVisible) {
            rightDrawable?.apply {
                setBounds(0, 0, minimumWidth, minimumHeight)
                binding.tvRight.setCompoundDrawables(null, null, this, null)
            }
        } else {
            binding.tvRight.setCompoundDrawables(null, null, null, null)

        }

    }

}